<?php
namespace app\admin\controller;

use think\Controller;
use app\admin\model\User;
use app\admin\model\Classes;
use app\admin\model\Term;
use app\admin\model\Course;
use app\admin\model\Winning;
use app\admin\model\Training;
use app\admin\model\Class_hour;
use app\admin\model\Research;
use app\admin\model\Paper;
use app\admin\model\Train;
use think\Db;
use think\Request;

class Admin extends Controller
{
	public function _initialize()
	{
		if (! session('userInfo.status')) {
			return $this->error('请登录', url('admin/Login/login'));
		}
		if (! session('userInfo.admin')==1) {
			return $this->error('您不是超级管理员！');
			exit;
		}
	}
	//学期管理
	public function index()
	{
		$term = Term::order('id','desc')->paginate(6);
		$Page = $term->render();
		$this->assign('page', $Page);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
		$this->assign('termList',$term);

		return $this->fetch();
	}
	public function term_add(Request $request)
	{
		$data = $request->param();
		if(empty($data['term'])){
			$this->error('学期不能为空！');
		}
		$res = Term::create($data);
		if ($res) {
			$this->redirect('Admin/index',302);
		} else {
			$this->error('添加失败');
		}
	}
	public function term_v(Request $request)
	{
		$id = $request->param('id');
		$term = Term::where('id',$id)->find();
		return json($term);
	}
	public function term_edit(Request $request)
	{
		$id = $request->param('id');
		$term = $request->param('term');
		if(empty($term)){
			$this->error('学期不能为空！');
		}
		$res = Term::where('id',$id)->update(['term'=>$term]);
		if(!$res==0){
			$this->redirect('Admin/index',302);
		}else{
			$this->error("数据未更改！");
		}
	}
	public function term_del(Request $request)
	{
		$id = $request->param('id');
		$res = Term::destroy($id);
		if ($res) {
			$this->redirect('Admin/index',302);
		} else {
			$this->error('删除失败');
		}
	}
	//班级管理
	public function banji()
	{
		$classes = Classes::order('id','desc')->paginate(6);
		$Page = $classes->render();
		$this->assign('page', $Page);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
		$this->assign('classesList',$classes);
		return $this->fetch();
	}
	public function class_add(Request $request)
	{
		$data = $request->param();
		if(empty($data['class_name'])){
			$this->error('班级不能为空！');
		}
		if(empty($data['class_nub'])){
			$this->error('人数不能为空！');
		}
		$res = Classes::create($data);
		if ($res) {
			$this->redirect('Admin/banji',302);
		} else {
			$this->error('添加失败');
		}
	}
	public function class_v(Request $request)
	{
		$id = $request->param('id');
		$classes = Classes::where('id',$id)->find();
		return json($classes);
	}
	public function class_edit(Request $request)
	{
		$id = $request->param('id');
		$name = $request->param('class_name');
		$nub = $request->param('class_nub');
		if(empty($name)){
			$this->error('班级不能为空！');
		}
		if(empty($nub)){
			$this->error('人数不能为空！');
		}
		$res = Classes::where('id',$id)->update(['class_name'=>$name,'class_nub'=>$nub]);
		if(!$res==0){
			$this->redirect('Admin/banji',302);
		}else{
			$this->error("数据未更改！");
		}
	}
	public function class_del(Request $request)
	{
		$id = $request->param('id');
		$res = Classes::destroy($id);
		if ($res) {
			$this->redirect('Admin/banji',302);
		} else {
			$this->error('删除失败');
		}
	}
	//课程管理
	public function kecheng()
	{
		$course = Course::order('id','desc')->paginate(6);
		$Page = $course->render();
		$this->assign('page', $Page);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
		$this->assign('courseList',$course);
		return $this->fetch();
	}
	public function kc_add(Request $request)
	{
		$data = $request->param();
		if(empty($data['course_name'])){
			$this->error('课程不能为空！');
		}
		$res = Course::create($data);
		if ($res) {
			$this->redirect('Admin/kecheng',302);
		} else {
			$this->error('添加失败');
		}
	}
	public function kc_v(Request $request)
	{
		$id = $request->param('id');
		$course = Course::where('id',$id)->find();
		return json($course);
	}
	public function kc_edit(Request $request)
	{
		$id = $request->param('id');
		$name = $request->param('course_name');
		if(empty($name)){
			$this->error('课程不能为空！');
		}
		$res = Course::where('id',$id)->update(['course_name'=>$name]);
		if(!$res==0){
			$this->redirect('Admin/kecheng',302);
		}else{
			$this->error("数据未更改！");
		}
	}
	public function kc_del(Request $request)
	{
		$id = $request->param('id');
		$res = Course::destroy($id);
		if ($res) {
			$this->redirect('Admin/kecheng',302);
		} else {
			$this->error('删除失败');
		}
	}
	//用户管理
	public function yohu()
	{
		$user = User::order('id','desc')->paginate(2);
		$Page = $user->render();
		$this->assign('page', $Page);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
		$this->assign('userList',$user);
		return $this->fetch();
	}
	public function u_search(Request $request)
	{
		$name = $request->param('search');
		$user = User::where('username','like','%'.$name.'%')->order('id','desc')->paginate(2,false,['query'=>request()->param()]);
		$Page = $user->render();
		$this->assign('page', $Page);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
		$this->assign('userList',$user);
		return $this->fetch('yohu');
	}
	public function yh_add(Request $request)
	{
		$username = $request->param('username');
		$email = $request->param('email');
		$admin = $request->param('admin');
		if(empty($username)){
			$this->error('用户名不能为空！');
		}
		if(empty($email)){
			$this->error('邮箱不能为空！');
		}
		$jm = md5('123456');
		$data=array(
			'username' => $username,
			'passwd' => $jm,
			'email' => $email,
			'admin' => $admin
		);
		$res = User::create($data);
		if ($res) {
			$this->redirect('Admin/yohu',302);
		} else {
			$this->error('添加失败');
		}
	}
	public function yh_v(Request $request)
	{
		$id = $request->param('id');
		$user = User::where('id',$id)->find();
		return json($user);
	}
	public function yh_edit(Request $request)
	{
		$id = $request->param('id');
		$name = $request->param('username');
		$admin = $request->param('admin');
		$email = $request->param('email');
		if(empty($name)){
			$this->error('用户名不能为空！');
		}
		if(empty($email)){
			$this->error('邮箱不能为空！');
		}
		$res = User::where('id',$id)->update(['username'=>$name,'admin'=>$admin,'email'=>$email]);
		if(!$res==0){
			$this->redirect('Admin/yohu',302);
		}else{
			$this->error("数据未更改！");
		}
	}
	public function yh_reset(Request $request)
	{
		$id = $request->param('id');
		$passwd = md5('123456');
		$model = User::get($id);
		$model->passwd=$passwd;
		$res = $model->save();
		if ($res) {
			$this->redirect('Admin/yohu',302);
		} else {
			$this->error('重置失败');
		}
	}
	public function yh_del(Request $request)
	{
		$id = $request->param('id');
		$res = User::destroy($id);
		if ($res) {
			$this->redirect('Admin/yohu',302);
		} else {
			$this->error('删除失败');
		}
	}
	//获奖统计
	public function winning()
	{
		$t = Term::order('id','desc')->select();
		$winning = Db::view('winning','id,user_id,term_id,winning,rating,winning_time,remark,state,create_time')
			->view('term','term','winning.term_id=term.id')
			->view('user','username','winning.user_id=user.id')
			->where('state','in','1,2')
			->order('id','desc')
			->paginate(2);

		$Page = $winning->render();
		$this->assign('page', $Page);
		$this->assign('tList', $t);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
		$this->assign('winningList',$winning);

		return $this->fetch();
	}
	public function w_xlxq($id)
	{
		$winning = Db::view('winning','id,user_id,term_id,winning,rating,winning_time,remark,state,create_time')
			->view('term','term','winning.term_id=term.id')
			->view('user','username','winning.user_id=user.id')
			->where('state','in','1,2')
			->where('term_id','=',$id)
			->order('id','desc')
			->select();

		return json($winning);
	}
	public function w_search(Request $request)
	{
		$name = $request->param('search');
		$term_id=$request->param('term_id');
		$t = Term::order('id','desc')->select();
		if($term_id=='选择学期'){
			$winning = Db::view('winning','id,user_id,term_id,winning,rating,winning_time,remark,state,create_time')
			->view('term','term','winning.term_id=term.id')
			->view('user','username','winning.user_id=user.id')
			->where('state','in','1,2')
			->where('username|term|winning|rating','like','%'.$name.'%')
			->order('id','desc')
			->paginate(2,false,['query'=>request()->param()]);
			$Page = $winning->render();
			$this->assign('page', $Page);
			$this->assign('tList', $t);
			$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
			$this->assign('winningList',$winning);
			return $this->fetch('winning');
		}else{
			$winning = Db::view('winning','id,user_id,term_id,winning,rating,winning_time,remark,state,create_time')
				->view('term','term','winning.term_id=term.id')
				->view('user','username','winning.user_id=user.id')
				->where('state','in','1,2')
				->where('term_id',$term_id)
				->where('username|term|winning|rating','like','%'.$name.'%')
				->order('id','desc')
				->paginate(2,false,['query'=>request()->param()]);
			$Page = $winning->render();
			$this->assign('page', $Page);
			$this->assign('tList', $t);
			$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
			$this->assign('winningList',$winning);
			return $this->fetch('winning');
		}
	}
	public function w_view(Request $request)
	{
		$id = $request->param('id');
		$winning = Db::view('winning','id,user_id,term_id,winning,rating,winning_time,remark,state,create_time')
			->view('term','term','winning.term_id=term.id')
			->view('user','username','winning.user_id=user.id')
			->where('id',$id)
			->find();
		return json($winning);
	}
	public function w_tg(Request $request)
	{
		$id = $request->param('id');
		$model = Winning::get($id);
		$model->state=2;
		$res = $model->save();
		if ($res) {
			$this->success('操作成功');
		} else {
			$this->error('操作失败');
		}
	}
	public function w_btg(Request $request)
	{
		$id = $request->param('id');
		$model = Winning::get($id);
		$model->state=3;
		$res = $model->save();
		if ($res) {
			$this->success('操作成功');
		} else {
			$this->error('操作失败');
		}
	}
	//实训统计
	public function training()
	{
		$t = Term::select();
		$training = Db::view('training','id,user_id,term_id,class_id,name,weeks,training_nub,state,create_time')
			->view('classes','class_name,class_nub','training.class_id=classes.id')
			->view('term','term','training.term_id=term.id')
			->view('user','username','training.user_id=user.id')
			->where('state','in','1,2')
			->order('id','desc')
			->paginate(2);

		$Page = $training->render();
		$this->assign('page', $Page);
		$this->assign('tList', $t);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
		$this->assign('trainingList',$training);

		return $this->fetch();
	}
	public function t_search(Request $request)
	{
		$name = $request->param('search');
		$term_id=$request->param('term_id');
		$t = Term::select();
		if($term_id=='选择学期'){
			$training = Db::view('training','id,user_id,term_id,class_id,name,weeks,training_nub,state,create_time')
			->view('classes','class_name,class_nub','training.class_id=classes.id')
			->view('term','term','training.term_id=term.id')
			->view('user','username','training.user_id=user.id')
			->where('state','in','1,2')
			->where('username|term|class_name|name','like','%'.$name.'%')
			->order('id','desc')
			->paginate(2,false,['query'=>request()->param()]);
			$Page = $training->render();
			$this->assign('page', $Page);
			$this->assign('tList', $t);
			$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
			$this->assign('trainingList',$training);
			return $this->fetch('training');
		}else{
			$training = Db::view('training','id,user_id,term_id,class_id,name,weeks,training_nub,state,create_time')
			->view('classes','class_name,class_nub','training.class_id=classes.id')
			->view('term','term','training.term_id=term.id')
			->view('user','username','training.user_id=user.id')
			->where('state','in','1,2')
			->where('term_id',$term_id)
			->where('username|term|class_name|name','like','%'.$name.'%')
			->order('id','desc')
			->paginate(2,false,['query'=>request()->param()]);
			$Page = $training->render();
			$this->assign('page', $Page);
			$this->assign('tList', $t);
			$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
			$this->assign('trainingList',$training);
			return $this->fetch('training');
		}
	}
	public function t_view(Request $request)
	{
		$id = $request->param('id');
		$training = Db::view('training','id,user_id,term_id,class_id,name,weeks,training_nub,state,create_time')
			->view('classes','class_name,class_nub','training.class_id=classes.id')
			->view('term','term','training.term_id=term.id')
			->view('user','username','training.user_id=user.id')
			->where('id',$id)
			->find();
		return json($training);
	}
	public function t_tg(Request $request)
	{
		$id = $request->param('id');
		$model = Training::get($id);
		$model->state=2;
		$res = $model->save();
		if ($res) {
			$this->success('操作成功');
		} else {
			$this->error('操作失败');
		}
	}
	public function t_btg(Request $request)
	{
		$id = $request->param('id');
		$model = Training::get($id);
		$model->state=3;
		$res = $model->save();
		if ($res) {
			$this->success('操作成功');
		} else {
			$this->error('操作失败');
		}
	}
	//授课统计
	public function sk()
	{
		$t = Term::select();
		$sk = Db::view('class_hour','id,user_id,term_id,class_id,course_id,plan_hour,hour,state,create_time')
			->view('classes','class_name,class_nub','class_hour.class_id=classes.id')
			->view('term','term','class_hour.term_id=term.id')
			->view('course','course_name','class_hour.course_id=course.id')
			->view('user','username','class_hour.user_id=user.id')
			->where('state','in','1,2')
			->order('id','desc')
			->paginate(2);

		$Page = $sk->render();
		$this->assign('page', $Page); 
		$this->assign('tList', $t);
		$this->assign('skList',$sk);

		return $this->fetch();
	}
	public function s_search(Request $request)
	{
		$name = $request->param('search');
		$term_id=$request->param('term_id');
		$t = Term::select();
		if($term_id=='选择学期'){
			$sk = Db::view('class_hour','id,user_id,term_id,class_id,course_id,plan_hour,hour,state,create_time')
			->view('classes','class_name,class_nub','class_hour.class_id=classes.id')
			->view('term','term','class_hour.term_id=term.id')
			->view('course','course_name','class_hour.course_id=course.id')
			->view('user','username','class_hour.user_id=user.id')
			->where('state','in','1,2')
			->where('username|term|class_name|class_nub|course_name|plan_hour|hour','like','%'.$name.'%')
			->order('id','desc')
			->paginate(2,false,['query'=>request()->param()]);
			$Page = $sk->render();
			$this->assign('page', $Page); 
			$this->assign('tList', $t);
			$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
			$this->assign('skList',$sk);
			return $this->fetch('sk');
		}else{
			$sk = Db::view('class_hour','id,user_id,term_id,class_id,course_id,plan_hour,hour,state,create_time')
			->view('classes','class_name,class_nub','class_hour.class_id=classes.id')
			->view('term','term','class_hour.term_id=term.id')
			->view('course','course_name','class_hour.course_id=course.id')
			->view('user','username','class_hour.user_id=user.id')
			->where('state','in','1,2')
			->where('term_id',$term_id)
			->where('username|term|class_name|class_nub|course_name|plan_hour|hour','like','%'.$name.'%')
			->order('id','desc')
			->paginate(2,false,['query'=>request()->param()]);
			$Page = $sk->render();
			$this->assign('page', $Page); 
			$this->assign('tList', $t);
			$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
			$this->assign('skList',$sk);
			return $this->fetch('sk');
		}
	}
	public function s_view(Request $request)
	{
		$id = $request->param('id');
		$sk = Db::view('class_hour','id,user_id,term_id,class_id,course_id,plan_hour,hour,state,create_time')
			->view('classes','class_name,class_nub','class_hour.class_id=classes.id')
			->view('term','term','class_hour.term_id=term.id')
			->view('course','course_name','class_hour.course_id=course.id')
			->view('user','username','class_hour.user_id=user.id')
			->where('id',$id)
			->find();
		return json($sk);
	}
	public function s_tg(Request $request)
	{
		$id = $request->param('id');
		$model = Class_hour::get($id);
		$model->state=2;
		$res = $model->save();
		if ($res) {
			$this->redirect('Admin/sk',302);
		} else {
			$this->error('操作失败');
		}
	}
	public function s_btg(Request $request)
	{
		$id = $request->param('id');
		$model = Class_hour::get($id);
		$model->state=3;
		$res = $model->save();
		if ($res) {
			$this->redirect('Admin/sk',302);
		} else {
			$this->error('操作失败');
		}
	}
	//科研项目统计
	public function research()
	{
		$t = Term::select();
		$research = Db::view('research','id,user_id,term_id,entry_name,project_time,closing_time,level,state,create_time')
			->view('term','term','research.term_id=term.id')
			->view('user','username','research.user_id=user.id')
			->where('state','in','1,2')
			->order('id','desc')
			->paginate(2);

		$Page = $research->render();
		$this->assign('page', $Page);
		$this->assign('tList', $t);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
		$this->assign('researchList',$research);

		return $this->fetch();
	}
	public function r_search(Request $request)
	{
		$name = $request->param('search');
		$term_id=$request->param('term_id');
		$t = Term::select();
		if($term_id=='选择学期'){
			$research = Db::view('research','id,user_id,term_id,entry_name,project_time,closing_time,level,state,create_time')
			->view('term','term','research.term_id=term.id')
			->view('user','username','research.user_id=user.id')
			->where('state','in','1,2')
			->where('username|term|entry_name|project_time|closing_time|level','like','%'.$name.'%')
			->order('id','desc')
			->paginate(2,false,['query'=>request()->param()]);
			$Page = $research->render();
			$this->assign('page', $Page);
			$this->assign('tList', $t);
			$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
			$this->assign('researchList',$research);
			return $this->fetch('research');
		}else{
			$research = Db::view('research','id,user_id,term_id,entry_name,project_time,closing_time,level,state,create_time')
			->view('term','term','research.term_id=term.id')
			->view('user','username','research.user_id=user.id')
			->where('state','in','1,2')
			->where('term_id',$term_id)
			->where('username|term|entry_name|project_time|closing_time|level','like','%'.$name.'%')
			->order('id','desc')
			->paginate(2,false,['query'=>request()->param()]);
			$Page = $research->render();
			$this->assign('page', $Page);
			$this->assign('tList', $t);
			$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
			$this->assign('researchList',$research);
			return $this->fetch('research');
		}
	}
	public function r_view(Request $request)
	{
		$id = $request->param('id');
		$research = Db::view('research','id,user_id,term_id,entry_name,project_time,closing_time,level,state,create_time')
			->view('term','term','research.term_id=term.id')
			->view('user','username','research.user_id=user.id')
			->where('id',$id)
			->find();
		return json($research);
	}
	public function r_tg(Request $request)
	{
		$id = $request->param('id');
		$model = Research::get($id);
		$model->state=2;
		$res = $model->save();
		if ($res) {
			$this->success('操作成功');
		} else {
			$this->error('操作失败');
		}
	}
	public function r_btg(Request $request)
	{
		$id = $request->param('id');
		$model = Research::get($id);
		$model->state=3;
		$res = $model->save();
		if ($res) {
			$this->success('操作成功');
		} else {
			$this->error('操作失败');
		}
	}
	//论文发表统计
	public function paper()
	{
		$t = Term::select();
		$paper = Db::view('paper','id,user_id,term_id,title,journal_name,publish,ranking_order,grade,state,create_time')
			->view('term','term','paper.term_id=term.id')
			->view('user','username','paper.user_id=user.id')
			->where('state','in','1,2')
			->order('id','desc')
			->paginate(2);

		$Page = $paper->render();
		$this->assign('page', $Page);
		$this->assign('tList', $t);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
		$this->assign('paperList',$paper);

		return $this->fetch();
	}
	public function l_search(Request $request)
	{
		$name = $request->param('search');
		$term_id=$request->param('term_id');
		$t = Term::select();
		if($term_id=='选择学期'){
			$paper = Db::view('paper','id,user_id,term_id,title,journal_name,publish,ranking_order,grade,state,create_time')
			->view('term','term','paper.term_id=term.id')
			->view('user','username','paper.user_id=user.id')
			->where('state','in','1,2')
			->where('username|term|title|journal_name|publish|ranking_order|grade','like','%'.$name.'%')
			->order('id','desc')
			->paginate(2,false,['query'=>request()->param()]);
			$Page = $paper->render();
			$this->assign('page', $Page);
			$this->assign('tList', $t);
			$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
			$this->assign('paperList',$paper);
			return $this->fetch('paper');
		}else{
			$paper = Db::view('paper','id,user_id,term_id,title,journal_name,publish,ranking_order,grade,state,create_time')
			->view('term','term','paper.term_id=term.id')
			->view('user','username','paper.user_id=user.id')
			->where('state','in','1,2')
			->where('term_id',$term_id)
			->where('username|term|title|journal_name|publish|ranking_order|grade','like','%'.$name.'%')
			->order('id','desc')
			->paginate(2,false,['query'=>request()->param()]);
			$Page = $paper->render();
			$this->assign('page', $Page);
			$this->assign('tList', $t);
			$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
			$this->assign('paperList',$paper);
			return $this->fetch('paper');
		}
	}
	public function l_view(Request $request)
	{
		$id = $request->param('id');
		$paper = Db::view('paper','id,user_id,term_id,title,journal_name,publish,ranking_order,grade,state,create_time')
			->view('term','term','paper.term_id=term.id')
			->view('user','username','paper.user_id=user.id')
			->where('id',$id)
			->find();
		return json($paper);
	}
	public function l_tg(Request $request)
	{
		$id = $request->param('id');
		$model = Paper::get($id);
		$model->state=2;
		$res = $model->save();
		if ($res) {
			$this->success('操作成功');
		} else {
			$this->error('操作失败');
		}
	}
	public function l_btg(Request $request)
	{
		$id = $request->param('id');
		$model = Paper::get($id);
		$model->state=3;
		$res = $model->save();
		if ($res) {
			$this->success('操作成功');
		} else {
			$this->error('操作失败');
		}
	}
	//培训(出差)统计
	public function train()
	{
		$t = Term::select();
		$train = Db::view('train','id,user_id,term_id,train_time,train_program,train_location,train_days,state,create_time')
			->view('term','term','train.term_id=term.id')
			->view('user','username','train.user_id=user.id')
			->where('state','in','1,2')
			->order('id','desc')
			->paginate(2);

		$Page = $train->render();
		$this->assign('page', $Page);
		$this->assign('tList', $t);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
		$this->assign('trainList',$train);

		return $this->fetch();
	}
	public function a_search(Request $request)
	{
		$name = $request->param('search');
		$term_id=$request->param('term_id');
		$t = Term::select();
		if($term_id=='选择学期'){
			$train = Db::view('train','id,user_id,term_id,train_time,train_program,train_location,train_days,state,create_time')
			->view('term','term','train.term_id=term.id')
			->view('user','username','train.user_id=user.id')
			->where('state','in','1,2')
			->where('username|term|train_time|train_program|train_location|train_days','like','%'.$name.'%')
			->order('id','desc')
			->paginate(2,false,['query'=>request()->param()]);
			$Page = $train->render();
			$this->assign('page', $Page);
			$this->assign('tList', $t);
			$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
			$this->assign('trainList',$train);
			return $this->fetch('train');
		}else{
			$train = Db::view('train','id,user_id,term_id,train_time,train_program,train_location,train_days,state,create_time')
				->view('term','term','train.term_id=term.id')
				->view('user','username','train.user_id=user.id')
				->where('state','in','1,2')
				->where('term_id',$term_id)
				->where('username|term|train_time|train_program|train_location|train_days','like','%'.$name.'%')
				->order('id','desc')
				->paginate(2,false,['query'=>request()->param()]);
			$Page = $train->render();
			$this->assign('page', $Page);
			$this->assign('tList', $t);
			$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
			$this->assign('trainList',$train);
			return $this->fetch('train');
		}
	}
	public function a_view(Request $request)
	{
		$id = $request->param('id');
		$train = Db::view('train','id,user_id,term_id,train_time,train_program,train_location,train_days,state,create_time')
			->view('term','term','train.term_id=term.id')
			->view('user','username','train.user_id=user.id')
			->where('id',$id)
			->find();
		return json($train);
	}
	public function a_tg(Request $request)
	{
		$id = $request->param('id');
		$model = Train::get($id);
		$model->state=2;
		$res = $model->save();
		if ($res) {
			$this->success('操作成功');
		} else {
			$this->error('操作失败');
		}
	}
	public function a_btg(Request $request)
	{
		$id = $request->param('id');
		$model = Train::get($id);
		$model->state=3;
		$res = $model->save();
		if ($res) {
			$this->success('操作成功');
		} else {
			$this->error('操作失败');
		}
	}
	//统计表格
	public function toji()
	{
		$t = Term::order('id','desc')->select();
		$this->assign('tList', $t);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');

		return $this->fetch();
	}
	//统计图
	public function toji2()
	{
		$q = (session('userInfo.id'));
		$t = Term::order('id','desc')->select();
		$u = User::order('id','desc')->select();
		$sub_w = Winning::where('user_id',$q)->count('winning'); 
		dump($sub_w);
		$this->assign('uList', $u);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
		$this->assign('tList', $t);

		return $this->fetch();
	}
	//统计团队
	public function tjz()
	{
		$t = Term::order('id','desc')->select();
		$this->assign('tList', $t);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');

		return $this->fetch();
	}
	public function user_class(Request $request)
	{
		$term_id=$request->param();
		$t = Term::select();
		$q = (session('userInfo.id'));
		$this->assign('tList', $t);
		$user=User::select();
		$username=Db::name('user')->where('id',$q)->value('username');
		dump($user);
	}
}